//
//  ConditionViewController.m
//  Audit
//
//  Created by cc on 15-2-12.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "ConditionViewController.h"

@interface ConditionViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UITableView *_tableView;
}
@end

@implementation ConditionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavTitle:self.naviTitle];
    [self setNavBackButton:nil];
    [self setNavRightButton:@"关闭"];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.dataFromServerKey=[NSMutableArray array];
    self.dataFromServerValue=[NSMutableArray array];
    
    if ([self.fromVC isEqualToString:@"addcar"]) {
        NSArray *arr=self.paramsArr;
        
        for (int i=0; i<arr.count; i++) {
            NSDictionary *dic=(NSDictionary *)[arr objectAtIndex:i];
            
            [self.dataFromServerKey addObject:[dic objectForKey:@"key"]];
            [self.dataFromServerValue addObject:[dic objectForKey:@"value"]];
        }
        
        [_tableView reloadData];
    }
    else if ([self.fromVC isEqualToString:@"searchcar"]) {
        NSArray *arr=self.paramsArr;
        
        [self.dataFromServerKey addObject:@""];
        [self.dataFromServerValue addObject:@"不限"];
        
        for (int i=0; i<arr.count; i++) {
            NSDictionary *dic=(NSDictionary *)[arr objectAtIndex:i];
            
            [self.dataFromServerKey addObject:[dic objectForKey:@"key"]];
            [self.dataFromServerValue addObject:[dic objectForKey:@"value"]];
        }
        
        [_tableView reloadData];
    }
//    else
//    {
////        [gAlert showLoading:nil delegate:nil];
////        [CommunicateWithService communicateWithServiceApiName:[self.paramsArr objectForKey:@"port"] finishSelector:@selector(finished:) failSelector:@selector(failed:) delegateObj:self];
//    }
}

//请求服务器成功
-(void)finished:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    if (request.responseStatusCode == 400) {
        //textView.text = @"Invalid code";
        NSLog(@"Invalid code");
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
        //textView.text = @"Code already used";
    } else if (request.responseStatusCode == 200) {
        
        NSDictionary *strDic=[CommunicateWithService getDataDic:request.responseString];//解析json 2 dic
        
        NSString *flag=[strDic objectForKey:@"resultCode"];//服务器返回代码
        
        NSString *msg=[strDic objectForKey:@"resultValue"];//服务器返回提示消息
        
        NSArray *strDicData=(NSArray *)[strDic objectForKey:@"resultData"];//服务器返回数据字典
        
        if ([flag isEqualToString:@"1001"]) {
            for (int i=0; i<strDicData.count; i++) {
                NSDictionary *dic=(NSDictionary *)[strDicData objectAtIndex:i];
                
                [self.dataFromServerKey addObject:[dic objectForKey:@"key"]];
                [self.dataFromServerValue addObject:[dic objectForKey:@"value"]];
                
                [_tableView reloadData];
            }
        }
        else if ([flag isEqualToString:@"1002"])
        {
            [gAlert show:msg inView:self.view delegate:nil];
        }
    }
}

//请求服务器失败
-(void)failed:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    [NetworkCheck checkNetwork:self];//检查网络
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)navRightButtonActionAtTag:(NSUInteger)tag
{
    //todo:返回数据
    [super backAction:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataFromServerValue.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.text = [self.dataFromServerValue objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:23.0];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //todo选择值传回
    if (self.setText) {
        self.setText([self.dataFromServerKey objectAtIndex:[indexPath row]],[self.dataFromServerValue objectAtIndex:[indexPath row]]);
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
@end
